package com.aduerr.v1shouyin.mvp.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.aduerr.v1shouyin.App;
import com.aduerr.v1shouyin.entity.CashEntity;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.mvp.base.BasePresenter;
import com.aduerr.v1shouyin.mvp.view.CashView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.internal.observers.BlockingBaseObserver;
import io.reactivex.schedulers.Schedulers;

import javax.inject.Inject;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.HashMap;

/**
 * 类名: CreatErweimaPresenter
 * 此类用途: ---
 *
 * @Author: peixi
 * @Date: 2017-12-07 09:45
 * @Email: cr7inmanchester.com
 * @FileName: com.aduer.shouyin.mvp.presenter.CreatErweimaPresenter.java
 */
public class CashPresenter extends BasePresenter<CashView> {
    @Inject
    public CashPresenter(App app) {
        super(app);
    }

    //扫码付款
    public void getCashresult(final Context mContext, String beizhu, String paymoney) {
        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("PayType", "0");
        addParams.put("payMoney", paymoney);
        addParams.put("bz", "" + beizhu);
//        getView().showProgress();
        getAppComponent().getAPIService().CashPay(RXRequest.getXingPosParams(addParams,
                mContext))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BlockingBaseObserver<CashEntity>() {
                    @Override
                    public void onNext(CashEntity cashEntity) {
                        if(isViewAttached())
                            getView().onCashView(cashEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(e instanceof SocketTimeoutException) {
                            Toast.makeText(App.getApp(), "网络连接超时", Toast.LENGTH_SHORT).show();
                        } else if(e instanceof ConnectException) {
                            Toast.makeText(App.getApp(), "连接失败", Toast.LENGTH_SHORT).show();
                        } else if(e instanceof UnknownHostException) {
                            Log.e("onError:", "error:-----" + e.getMessage());//R
                            // .string.checknet +
                            Toast.makeText(App.getApp(), "检查网络状态", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.e("onError:", "error:" + e.getMessage());//R.string
                            // .checknet +
                            Toast.makeText(App.getApp(), "检查网络状态", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }

}
